/*
 * 作者:熊猫
 * 日期:2017 - 3 - 19 6 - 53 - 5
 *
 */

package net.lainiao.morethread.Chapter7.demo6;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;

/**
 * Created by Administrator on 2017/3/19.
 */
public class MyWorkerThread extends ForkJoinWorkerThread {
    /**
     * Creates a ForkJoinWorkerThread operating in the given pool.
     *
     * @param pool the pool this thread works in
     * @throws NullPointerException if pool is null
     */
    protected MyWorkerThread(ForkJoinPool pool) {
        super(pool);
    }

    private static ThreadLocal<Integer> taskCounter =new InheritableThreadLocal<>();

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("MyWorkerThread("+getId()+") initializing task counter ");
        taskCounter.set(0);
    }

    @Override
    protected void onTermination(Throwable exception) {
        System.out.println("MyWorkerThread("+getId()+") onTermination :"+taskCounter.get());
        super.onTermination(exception);
    }

    public void addTask(){
        int counter=taskCounter.get().intValue();
        counter++;
        taskCounter.set(counter);
    }


}
